<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="nav-bar">
			<uni-icons type="back" size="28" color="#fff" @click="goBack" />
			<text class="title">导航</text>
			<view class="right-icons"></view>
		</view>

		<!-- 地图区域 -->
		<view class="map-container">
			<map class="map" 
				:latitude="latitude" 
				:longitude="longitude"
				:markers="markers"
				:polyline="polyline"
				:scale="14"
				show-location>
			</map>
		</view>

		<!-- 导航信息卡片 -->
		<view class="nav-info-card">
			<view class="station-info">
				<view class="station-name">{{ stationName }}</view>
				<view class="station-address">{{ stationAddress }}</view>
			</view>
			<view class="nav-actions">
				<button class="nav-btn" @click="startNavigation">开始导航</button>
				<button class="nav-btn secondary" @click="openMapApp">打开地图APP</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				latitude: 0,
				longitude: 0,
				stationName: '',
				stationAddress: '',
				markers: [],
				polyline: [],
				startLat: 0,
				startLng: 0
			}
		},
		onLoad(options) {
			// 获取传递的参数
			if (options.latitude && options.longitude) {
				this.latitude = parseFloat(options.latitude);
				this.longitude = parseFloat(options.longitude);
				this.stationName = options.name || '加油站';
				this.stationAddress = options.address || '';
			}

			// 获取当前位置
			this.getCurrentLocation();
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			// 获取当前位置
			getCurrentLocation() {
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						this.startLat = res.latitude;
						this.startLng = res.longitude;
						this.updateMarkers();
					},
					fail: (err) => {
						console.error('获取位置失败：', err);
						uni.showToast({
							title: '获取位置失败',
							icon: 'none'
						});
					}
				});
			},
			// 更新地图标记
			updateMarkers() {
				this.markers = [
					{
						id: 1,
						latitude: this.startLat,
						longitude: this.startLng,
						iconPath: '../../../static/当前位置.png',
						width: 30,
						height: 30,
						callout: {
							content: '我的位置',
							color: '#fff',
							bgColor: '#2e8bff',
							borderRadius: 4,
							padding: 4
						}
					},
					{
						id: 2,
						latitude: this.latitude,
						longitude: this.longitude,
						iconPath: '../../../static/定位.png',
						width: 30,
						height: 30,
						callout: {
							content: this.stationName,
							color: '#fff',
							bgColor: '#ff9800',
							borderRadius: 4,
							padding: 4
						}
					}
				];
			},
			// 开始导航
			startNavigation() {
				// 使用腾讯地图导航
				uni.openLocation({
					latitude: this.latitude,
					longitude: this.longitude,
					name: this.stationName,
					address: this.stationAddress,
					success: () => {
						console.log('打开导航成功');
					},
					fail: (err) => {
						console.error('打开导航失败：', err);
						uni.showToast({
							title: '打开导航失败',
							icon: 'none'
						});
					}
				});
			},
			// 打开第三方地图APP
			openMapApp() {
				// 检查是否安装了地图APP
				uni.getSystemInfo({
					success: (res) => {
						// 根据平台选择打开不同的地图APP
						if (res.platform === 'android') {
							// Android平台
							uni.openApp({
								appId: 'com.baidu.BaiduMap',
								path: `baidumap://map/direction?destination=latlng:${this.latitude},${this.longitude}|name:${this.stationName}`,
								fail: () => {
									uni.showToast({
										title: '请先安装百度地图',
										icon: 'none'
									});
								}
							});
						} else if (res.platform === 'ios') {
							// iOS平台
							uni.openApp({
								appId: 'baidumap',
								path: `baidumap://map/direction?destination=latlng:${this.latitude},${this.longitude}|name:${this.stationName}`,
								fail: () => {
									uni.showToast({
										title: '请先安装百度地图',
										icon: 'none'
									});
								}
							});
						}
					}
				});
			}
		}
	}
</script>

<style scoped>
	.container {
		/* #ifdef MP-WEIXIN */
		margin-top: 70rpx;
		/* #endif */
		background: linear-gradient(180deg, #2e8bff, #3ca7fa);
		min-height: 100vh;
		background-repeat: no-repeat;
		background-size: 100% 35%;
	}

	.nav-bar {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 40rpx 24rpx 24rpx 24rpx;
	}

	.title {
		color: #fff;
		font-size: 40rpx;
	}

	.right-icons {
		display: flex;
		width: 28px;
	}

	.map-container {
		background: #fff;
		border-radius: 24rpx;
		margin: 24rpx;
		padding: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
	}

	.map {
		width: 100%;
		height: 600rpx;
		border-radius: 16rpx;
	}

	.nav-info-card {
		background: #fff;
		border-radius: 24rpx;
		margin: 24rpx;
		padding: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
	}

	.station-info {
		margin-bottom: 30rpx;
	}

	.station-name {
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.station-address {
		font-size: 28rpx;
		color: #666;
	}

	.nav-actions {
		display: flex;
		gap: 20rpx;
	}

	.nav-btn {
		flex: 1;
		background: linear-gradient(90deg, #2e8bff, #3ca7fa);
		color: #fff;
		font-size: 32rpx;
		border-radius: 40rpx;
		padding: 20rpx 0;
		border: none;
	}

	.nav-btn.secondary {
		background: linear-gradient(90deg, #ff9800, #ff5722);
	}
</style>
